var conData = require('MinesConstants');
cc.Class({
    extends: cc.Button,
    properties: {
        state:{
            default:conData.MINE,//雷
            type:cc.Integer
        },
        coordLabel:cc.Label,
        mineNumLabel:cc.Label,
        mineSp:cc.Sprite,
        flagSp:cc.Sprite,
        selPlayerPrefab:cc.Prefab,
    },

    // use this for initialization
    onLoad: function () {
        this._hasShow = false;
        this.row = -1;
        this.col = -1;
        this.touchBeganTime_msec = 0;
        //默认没有被翻开
        this.isOpen = false;
        this.mineNumLabel.node.active = false;
        this.coordLabel.node.active = false;
        this.mineSp.enabled = false;
        this.flagSp.enabled = false;
        this.selPlayerList = [];
        this.hahhaha = 1;
        //this.addSelPlayer(null,null);
    },

    setCoord:function(row,col,state)
    {
        this.row = row;
        this.col = col;
        this.setMineNum(state);
        var coodStr = "("+row+","+col+")";
        this.coordLabel.string = coodStr;
    },

    setMineNum: function (num) {
        this.state  = num;
        var str = "";
        if(num > 0)
        {
            var rnd = Math.random() > 0.6;
            if(num == 1)
            {
                str = rnd ?  num+'+' : num ;
            }
            else if(num == 6)
            {
                str = rnd ?  num+'-' : num;
            }
            else
            {
                str = num;
                if(rnd)
                {
                    if(Math.random() > 0.5)
                    {
                        str = Math.random() > 0.5 ? (num-1)+'+' : num+'-';
                    }
                    else
                    {
                        str = Math.random() > 0.5 ? num+'+' : (num+1)+'-';
                    }
                }
            }
        }
        this.mineNumLabel.string = str;
        this.mineNumLabel.node.active = false;
        this.coordLabel.node.active = false;
    },

    open: function () {
        //被标记过
        if(this.isMarked())
        {
            return;
        }

        this.isOpen = true;
        this.node.opacity = 150;
        if(this.state == conData.MINE)
        {
            this.mineSp.enabled = true;
        }
        else
        {
            this.mineNumLabel.string = this.state;
            this.mineNumLabel.node.active = (this.state > 0);
        }
    },

    isMarked: function () {
      return  this.flagSp.enabled;
    },

    mark: function () {
        var isEnable = this.flagSp.enabled;
        this.flagSp.enabled = !isEnable;
    },

    addSelPlayer:function(selFrame,isMarkLine,usrID)
    {
        var selPlayerNode = cc.instantiate(this.selPlayerPrefab);
        this.node.parent.addChild(selPlayerNode,100);

        var selPlayerCon = selPlayerNode.getComponent(require('SelPlayer'));
        selPlayerCon.head.spriteFrame = selFrame;
        selPlayerCon.mine.active = isMarkLine;
        selPlayerCon.usrID = usrID;
        this.selPlayerList.push(selPlayerCon);
        var selSize = this.selPlayerList.length;
        var pos = this.node.getPosition();
        if(selSize == 1)
        {
            selPlayerNode.setPosition(pos);
        }
        else
        {
            var deltaRadian = 2*Math.PI/selSize;
            var nodeSize = this.node.getContentSize();
            for(var i=0;i<this.selPlayerList.length;i++)
            {
                var node = this.selPlayerList[i].node;
                var radian = i*deltaRadian+Math.PI*0.5;
                var addPos = cc.p(Math.sin(radian)*nodeSize.width*0.2,Math.cos(radian)*nodeSize.height*0.2);
                var newPos = cc.pAdd(pos, addPos);
                node.setPosition(newPos);
                node.setScale(0.7);
            }
        }
    },

    calcScore: function(usrID,addScore,deductCoin,isMarkMine)
    {
        for(var i=0;i<this.selPlayerList.length;i++)
        {
            var selPlayer = this.selPlayerList[i];
            if(selPlayer.usrID == usrID)
            {
                selPlayer.scoreLabel.string = '+'+addScore;
                selPlayer.scoreLabel.node.active = true;
                var fadeOut = cc.fadeOut(1.0);
                var delayTime = cc.delayTime(0.3);
                var seq = cc.sequence(delayTime,fadeOut,cc.removeSelf());
                selPlayer.node.runAction(seq);
                this.selPlayerList.splice(i,1);
                break;
            }
        }
    },

    _onTouchBegan: function (event)
    {
        if(this.isOpen) return;
        this.touchBeganTime_msec = new Date().getTime();
        var curWorldPos = this.node.parent.convertToWorldSpaceAR(this.node.getPosition());
        var touchPos = event.touch.getLocation ();
        if(this.isInTouch(curWorldPos,touchPos))
        {
            this._super(event);
        }
    },


    _onMouseMoveIn: function (event)
    {
        if(this.isOpen) return;
        this._super(event);
        //var curWorldPos = this.node.parent.convertToWorldSpaceAR(this.node.getPosition());
        //var touchPos = event.getLocation ();
        //console.log(curWorldPos+ "_onMouseMoveIn" + touchPos.x + "  "+touchPos.y);
        //if(this.isInTouch(curWorldPos,touchPos))
        //{
        //    this._super(event);
        //}
    },


    isInTouch:function(hexWorldPos,touchPos)
    {
        var vec = cc.pSub(touchPos,hexWorldPos);
        var angle = cc.pAngle(vec,cc.p(1,0));
        if(vec.y < 0) angle = 6.2831853 - angle;
        var posLis = this.getTraingle2PosByAngle(angle)
        var SIDE_LEN = 40;
        //点在三角形内判断http://blog.csdn.net/pdcxs007/article/details/51436483
        var a = hexWorldPos;
        var p = touchPos;
        var b = cc.pAdd(a,cc.pMult(posLis[0],SIDE_LEN));
        var c = cc.pAdd(a,cc.pMult(posLis[1],SIDE_LEN));
        //console.log(a + " _ " + b + " _ "+c + " _ ("+ p.x+ ","+ p.y+")");
        //计算三角形的方向：
        var signOfTrig = (b.x - a.x)*(c.y - a.y) - (b.y - a.y)*(c.x - a.x);
        //再分别计算P在AB、CA、BC的哪一侧：
        var signOfAB = (b.x - a.x)*(p.y - a.y) - (b.y - a.y)*(p.x - a.x);
        var signOfCA = (a.x - c.x)*(p.y - c.y) - (a.y - c.y)*(p.x - c.x);
        var signOfBC = (c.x - b.x)*(p.y - b.y) - (c.y - b.y)*(p.x - b.x);
        //console.log(signOfTrig + " _ "+signOfAB + " _ "+signOfCA + " _ "+signOfBC )
        var d1 = (signOfAB * signOfTrig > 0);
        var d2 = (signOfCA * signOfTrig > 0);
        var d3 = (signOfBC * signOfTrig > 0);
        //console.log(d1 + "  " + d2 + "  "+d3);
        return (d1 && d2 && d3)
    },


    getTraingle2PosByAngle:function(angle)
    {
        var pi_60 = 1.0471976;
        var correctAngle = angle+pi_60*0.5;
        if(correctAngle > 6*pi_60) correctAngle = correctAngle - 6*pi_60;
        var nums_60 = correctAngle / pi_60;
        if(nums_60 < 1)
        {
            return [cc.p(0.866,-0.5),cc.p(0.866,0.5)];
        }
        else if(nums_60 < 2)
        {
            return [cc.p(0.866,0.5),cc.p(0,1)];
        }
        else if(nums_60 < 3)
        {
            return [cc.p(0,1),cc.p(-0.866,0.5)];
        }
        else if (nums_60 < 4)
        {
            return [cc.p(-0.866,0.5),cc.p(-0.866,-0.5)];
        }
        else if(nums_60 < 5)
        {
            return [cc.p(-0.866,-0.5),cc.p(0,-1)];
        }
        return [cc.p(0,-1),cc.p(0.866,-0.5)];
    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
